//
//  HJSellViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJSellViewController.h"
#import "HJAskToBuyCell.h"
#import "HJSellOderDetailController.h"
#import "HJMineModel.h"
#import "HJInputViewController.h" // 录入单号
@interface HJSellViewController ()
@property(nonatomic,strong)NSMutableArray <HJSellModel *>* dataArray;
@end

@implementation HJSellViewController
-(NSMutableArray <HJSellModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNetworkData];
    [mNotificationCenter addObserver:self selector:@selector(inputNumberNotification:) name:@"inputNumberNotification" object:nil];
}

-(void)configTableView {
    self.needRefresh = YES;
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    [self.tableView registerCellByNibName:@"HJAskToBuyCell"];
}
-(void)loadNetworkData {
    [TZHttpTool post:SellAPI params:@{@"page":@(self.page)} success:^(NSDictionary *result) {
        NSArray * arr =[HJSellModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"models"]];
        if (self.page==1) {
            [self.dataArray removeAllObjects];
        }
        self.totalPage = result[@"data"][@"totalPage"];
        [self.dataArray addObjectsFromArray:arr];
        [self.tableView reloadData];
        [self.tableView endRefresh];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataArray.count>0) {
        [tableView hideNoData];
        return self.dataArray.count;
    }else {
        [tableView showNoData];
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJAskToBuyCell * askCell = [tableView dequeueReusableCellWithIdentifier:@"HJAskToBuyCell"];
    askCell.selectionStyle = UITableViewCellSelectionStyleNone;
    askCell.sellModel = self.dataArray[indexPath.row];
    
    return askCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJSellModel * model = self.dataArray[indexPath.row];
    return model.cellHeight;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HJSellOderDetailController * sellOderDetailVC =[[HJSellOderDetailController alloc]init];
    HJSellModel * model = self.dataArray[indexPath.row];
    sellOderDetailVC.navTitle = @"订单详情";
    sellOderDetailVC.sell_id = model.sell_id;
    [self.navigationController pushViewController:sellOderDetailVC animated:YES];
}
#pragma mark -- 录入单号
-(void)inputNumberNotification:(NSNotification *)not {
    HJInputViewController * inputVC =[[HJInputViewController alloc]init];
    inputVC.sell_id = not.userInfo[@"sell_id"];
    [self.navigationController pushViewController:inputVC animated:YES];
}
@end
